﻿
@{
    ViewBag.Title = "退药查询";
    Layout = "~/Views/Shared/_Index.cshtml";
    var reportUrl = Newtouch.Core.Common.Utils.ConfigurationHelper.GetAppConfigValue("ReportUrl");
    var reportSystemCode = Newtouch.Core.Common.Utils.ConfigurationHelper.GetAppConfigValue("ReportSystemCode");
    var curOpr = Newtouch.Common.Operator.OperatorProvider.GetCurrent();
}


@using Newtouch.Infrastructure;
<script src="@SiteUrl.GetStaticResourceScriptUrl("~/js/wdtree/tree.js")"></script>
<link href="@SiteUrl.GetStaticResourceScriptUrl("~/js/wdtree/tree.css")" rel="stylesheet" />

<style>
    .dv-left {
        width: 200px;
        float: left;
    }

    .dv-left-tree {
        overflow-y: auto;
        border-radius: 4px;
        background: white;
    }

    .dv-left-title {
        padding: 6px 10px;
        border-bottom: 1px solid transparent;
        border-top-left-radius: 3px;
        border-top-right-radius: 3px;
        border-color: #ddd;
        background-color: white;
    }

    .dv-right {
        float: left;
        margin-left: 5px;
    }

    .dv-right-order {
        width: 100%;
    }

    .dv-grid {
        width: 100%;
    }
</style>


<div class="dv-body">
    <div class="dv-left">
        <div class="panel panel-default">
            <div class="panel-heading navb-bg">
                筛选条件
            </div>
            <table class="form" style="width:95%">
                <tbody>
                    <tr>
                        <th class="formTitle">姓名/住院号：</th>
                        <td class="formValue">
                            <input id="keyword" type="text" class="form-control">
                        </td>
                    </tr>
                </tbody>
            </table>
        </div>
        <div class="dv-left-title">
            患者列表
        </div>
        <div class="dv-left-tree" id="PatTreeList"></div>
    </div>
    <div class="dv-right">
        <div class="dv-right-order">
            <div class="panel panel-default">
                <div class="panel-heading navb-bg">
                    筛选条件

                </div>
                <div>
                    <table class="form">
                        <tr>
                            <th class="formTitle">药品名称：</th>
                            <td class="formValue">
                                <input id="txt_name" type="text" class="form-control" placeholder="药品名称" />
                            </td>
                            <th class="formTitle">开始日期：</th>
                            <td class="formValue">
                                <input id="txt_ksrq" type="text" class="form-control input-wdatepicker formClearIgnore" onclick="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd', onpicked: function () { refleshMedList(); } })" value="@DateTime.Now.AddDays(-7).ToString("yyyy-MM-dd")" />
                            </td>
                            <th class="formTitle">结束日期：</th>
                            <td class="formValue">
                                <input id="txt_jsrq" type="text" class="form-control input-wdatepicker formClearIgnore" onclick="WdatePicker({ dateFmt: 'yyyy-MM-dd' })" onfocus="WdatePicker({ dateFmt: 'yyyy-MM-dd', onpicked: function () { refleshMedList(); } })" value="@DateTime.Now.AddDays(1).ToString("yyyy-MM-dd")" />
                            </td>
                            <td class="formTitle">
                                <input type="button" id="btn_search" class="btn btn-md btn-primary" value="查询" />
                            </td>
                            @*<td class="formTitle">
                            <input type="button" id="btn_confirm" class="btn btn-md btn-primary" value="退药确认" />
                        </td>*@
                        </tr>
                    </table>
                </div>
            </div>
            <div class="gridPanel">
                <table id="gridList"></table>
            </div>
            <div id="tree"></div>
            @Html.Partial("_BottomButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel()
            {
                ShowKeyList = new int[] {8},
                F8Text="打印退药单"
            })
        </div>
    </div>
</div>


<script type="text/javascript">
    var checkArray = [];
    $(".dv-body").height($(window).height());
    $(".dv-body").width($(window).width());
    var rightWidth = $(".Newtouch_iframe", parent.document).width() - $(".dv-left").width() - 25;
    $(".dv-right").width(rightWidth);
    $(".dv-left-tree").height($(window).height() - 70);
    var keyword = $("#txt_name").val();
    var kssj = $("#txt_ksrq").val();
    var jssj = $("#txt_jsrq").val();
    var zyh = "";
    
    $(function () {
        $(window).resize(function () {//浏览器窗口调整大小时重新计算宽高
            $(".dv-body").height($(window).height());
            $(".dv-body").width($(window).width());
        });
        $('#gridList').jqGrid("clearGridData");

        getPatTreeList();
        gridList("");

        //搜索病人信息
        $('#keyword').keyup(function () {
            getPatTreeList();
        });

    });

    function getPatTreeList() {


        $("#PatTreeList").treeview({
            height: $(window).height() - 175,
            slimscroll: false,
            showcheck: false,
            url: "/NurseManage/InpatientMedReturn/Treecx?keyword=" + $('#keyword').val(),
            onnodeclick: function (item) {
                if (item.value && item.Ex1 == "c") {
                    checkArray = [];
                    zyh = item.val;
                    $('#gridList').jqGrid('setGridParam', {
                        postData: { patInfo: item.value, keyword: keyword, kssj: kssj, jssj: jssj }
                    }).trigger("reloadGrid");
                }

            }
        });

        $("#btn_search").click(function () {
            keyword = $("#txt_name").val();
            kssj = $("#txt_ksrq").val();
            jssj = $("#txt_jsrq").val();
            var patcheck = $("#PatTreeList").getCurrentNode();
            if (patcheck.Ex1 == "c") {
                $("#gridList").jqGrid('setGridParam', {
                    postData: { patInfo: patcheck.value, keyword: keyword, kssj: kssj, jssj: jssj },
                }).trigger('reloadGrid');
            }

        });
    }

    //回车事件
    $('#txt_name').keydownEnterEvent(function () {
        $('#btn_search').trigger('click');
    })

    //刷新列表
    function refleshMedList() {
        $("#btn_search").trigger('click');
    }

    //function refleshMedList() {
    //    $("#btn_search").trigger('click');
    //}

    var checkTrigger = false;
    var list = new Array();
    var liststu = false;
    var j = 0;
    function gridList(patzyh) {

        var $gridList = $("#gridList");
        $gridList.dataGrid({
            postData: { patList: patzyh, keyword: keyword, kssj: kssj, jssj: jssj },
            url: "/NurseManage/InpatientMedReturn/Griddata",
            editurl: "clientArray",
            height: $(window).height() - 170,
            colModel: [
                { label: "主键", name: "Id", hidden: true, key: true },
                { label: '姓名', name: 'hzxm', width: 100, align: 'left' },
                { label: '退药申请单号', name: 'tydh', width: 80, align: 'left' },
                { label: '床位名称', name: 'cwmc', width: 80, align: 'left' },
                { label: '医嘱性质', name: 'yzxz', width: 60, align: 'center', formatter: function (cellvalue) { if (cellvalue == 2) return "长期"; if (cellvalue == 1) return "临时"; } },
                { label: '药品名称', name: 'ypmc', width: 250, align: 'center' },
                { label: '退药数量', name: 'tysl', width: 60, align: 'center' },
                { label: '退药单位', name: 'ypdw', width: 60, align: 'center' },
                { label: '退药申请时间', name: 'CreateTime', width: 120, align: 'left' }, 
                { label: '退药记账时间', name: 'zxrq', width: 120, align: 'left' }, 
                { label: '操作人', name: 'ysxm', width: 120, align: 'left' },
                { label: '执行日期', name: 'zxrq', hidden: true },
                { label: '药房是否审核', name: 'zt', hidden: true },
                { label: 'zyh', name: 'zyh', hidden: true },
            ],
            viewrecords: true,
            multiselect: true,
            beforeSelectRow: function (rowid, e) {
                var $myGrid = $(this),
                    i = $.jgrid.getCellIndex($(e.target).closest('td')[0]),
                    cm = $myGrid.jqGrid('getGridParam', 'colModel');
                return (cm[i].name === 'cb');
            },
            onSelectAll: function (rowIds, status) {
            },
            onSelectRow: function (rowIndex, s) {

                if (!checkTrigger) {
                    var len = 0;
                    var rowData = $("#gridList").jqGrid('getRowData', rowIndex);
                    var ids = $("#gridList").jqGrid('getRowData_AllLine');

                    liststu = s; //同步状态
                    for (var i = 0; i < ids.length; i++) {
                        if (ids[i].tydh == rowData.tydh && ids[i].tydh != "") {
                            if (s == true) {
                                if (rowIndex != ids[i].Id) {
                                    list[len] = ids[i].Id;
                                        len += 1;
                                    }
                                }
                                else {
                                if (rowIndex != ids[i].Id) {
                                    list[len] = ids[i].Id;
                                        len += 1;
                                    }
                                }
                            }

                    }
                    checkTrigger = true;
                }

                if (list.length > 0) {

                    for (j = j; j < list.length; j++) {
                        if (liststu) //同组选中
                        {
                            j = j + 1;
                            $("#gridList").jqGrid("setSelection", list[j - 1], true);

                        }
                        else { //同组取消选中
                           // j = j + 1;
                            $("#gridList").jqGrid("setSelection", list[j], false);      //取消不触发onselectrow
                        }

                    }
                }

                j = 0;
                checkTrigger = false;
                list = new Array();
                liststu = false;  
            },
        });

    }

    function newtouch_event_f8()
    {
        var checkarr = [];

        var rowIds = $("#gridList").jqGrid('getGridParam', 'selarrrow');

        rowIds.forEach(item => {
            var rowData = $("#gridList").jqGrid('getRowData', item);
            checkarr.push(rowData);
        });

        var czIdStr = checkarr.map((item) => {
            return item.Id
        }).join(",");

        var zyh = checkarr[0].zyh;
        var tydh = "";
        for (var i = 0; i < checkarr.length; i++) {
            if (tydh.indexOf(checkarr[i].tydh)===-1) {
                tydh += checkarr[i].tydh+",";
            }
        }
        tydh = tydh.substring(0, tydh.length - 1);
        for (var i = 0; i < tydh.split(',').length; i++) {
            var values = new Array("@ViewBag.OrganizeId", zyh, czIdStr, "@ViewBag.OrganizeId", tydh.split(',')[i]);
            var keys = new Array("OrganizeId", "zyh", "czIds", "orgId", "tydh");

            var uri = '@reportUrl' + "?tempCode=46" + "&systemCode=" + '@reportSystemCode' + "&orgId=" + '@ViewBag.OrganizeId' + "&zyh=" + zyh + "&czIds=" + czIdStr + "&tydh=" + tydh.split(',')[i];
            window.open(uri);
            //openWindowWithPost("@(ViewBag.ReportServerHOST)/ReportServer/Pages/ReportViewer.aspx?%2fNewtouch.Report.1%2fTYCXDY&rs:Command=Render", "", keys, values);
        }
    }

    function openWindowWithPost(url, name, keys, values) {
        var newWindow = window.open(url, name);
        if (!newWindow) return false;
        var html = "";
        html += "<html><head></head><body><form id='formid' method='post' action='" + url + "'>";

        if (keys && values && (keys.length == values.length))
            for (var i = 0; i < keys.length; i++)
                html += "<input type='hidden' name='" + keys[i] + "' value='" + values[i] + "'/>";

        html += "</form><script type='text/javascript'>document.getElementById(\"formid\").submit()</sc" + "ript></body></html>";
        
        newWindow.document.write(html);
        return newWindow;
    }


</script>

